/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getCovereds;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.DestructionEvent;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.OccurrenceSpecification;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Determines the preferred bounds for a stop fragment.
 * <p>
 * The following attributes must be set before the command can run successfully:
 * </p>
 * <ul>
 * <li>AnyEditPart
 * </ul>
 * <p>
 * If addition, one of the following attributes must be set:
 * </p>
 * <ul>
 * <li>Stop - if set, the covered lifeline is derived from Stop#getCovereds()
 * and preferred bounds are calculated accordingly.
 * <li>Lifeline - if set, preferred bounds are calculated with respect to this
 * lifeline.
 * </ul>
 * <p>
 * When completed, the following attributes can be read to access the result:
 * </p>
 * <ul>
 * <li>PreferredBounds
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class DecideStopBoundsCommand extends Command {

	private OccurrenceSpecification stopOccurrence;
	private EditPart anyEditPart;
	private Lifeline coveredLifeline;
	private Rectangle preferred;

	public void setStop(final OccurrenceSpecification stop) {
		if (!(stop.getEvent() instanceof DestructionEvent))
			throw new IllegalArgumentException(
					"OccurrenceSpecification.Event != DestructionEvent");
		this.stopOccurrence = stop;
	}

	public void setAnyEditPart(final EditPart editPart) {
		this.anyEditPart = editPart;
	}

	public void setLifeline(final Lifeline lifeline) {
		this.coveredLifeline = lifeline;
	}

	public Rectangle getPreferredBounds() {
		return preferred;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		if (coveredLifeline != null
				|| (stopOccurrence != null && !getCovereds(stopOccurrence)
						.isEmpty())) {
			// Find the fragment figure via the EditPart Registry
			// and ask it for the preferred bounds

			final Lifeline lifeline = (coveredLifeline != null) ? coveredLifeline
					: getCovereds(stopOccurrence).get(0);
			GraphElement viewElement = (GraphElement) anyEditPart.getModel();
			List<GraphNode> listLifeline = LookForElement
					.lookForGraphElementWODiagram(lifeline, LookForElement
							.getDiagram(viewElement), new ArrayList<GraphNode>(
							1));
			GraphNode lifelineView = listLifeline.get(0);
			Rectangle coveredBounds = new Rectangle(lifelineView.getPosition(),
					lifelineView.getSize());

			int prefx = coveredBounds.x + coveredBounds.width / 2;
			int prefy = coveredBounds.bottom();
			preferred = new Rectangle(prefx, prefy, 1, 1);
			
			List<GraphNode> listStop = 
				LookForElement.lookForGraphElementWODiagram(
						stopOccurrence, 
						LookForElement.getDiagram(viewElement),
						new ArrayList<GraphNode>(1));
			
			// Set StopOccurence position
			if (! listStop.isEmpty()) {
				GraphNode stopView = listStop.get(0);
				stopView.setPosition(new Point(prefx, prefy));
			}
		}
	}

	@Override
	public void redo() {
		execute();
	}

	public void dispose() {
		stopOccurrence = null;
		coveredLifeline = null;
		anyEditPart = null;
		preferred = null;
	}
}
